package com.example.qiaoruian.shequ;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.qiaoruian.shequ.fragment.CommercialFragment;
import com.example.qiaoruian.shequ.fragment.HomeFragment;
import com.example.qiaoruian.shequ.fragment.LifestylesFragment;
import com.example.qiaoruian.shequ.fragment.ServcesFragment;
import com.example.qiaoruian.shequ.utils.CheckNet;
import com.example.qiaoruian.shequ.view.NoScrollViewPager;

public class MainActivity extends BaseActivity {

    private RadioButton rbDevice;
    private RadioButton rbDiscover;
    private RadioButton rbHospital;
    private RadioButton rbMedicine;
    private RadioGroup mainMenu;
    private NoScrollViewPager vp_main;
    private int viewpageSize = 4;
    private int checkPreId = R.id.rb_device;
    private HomeFragment mHomeFragment;
    private LifestylesFragment mLifestylesFragment;
    private ServcesFragment mServcesFragment;
    private CommercialFragment mCommercialFragment;
    private RadioButton[] rb;
    private Drawable[] drawables;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       //检查网络连接状态
        if(CheckNet.getNetState(this)==CheckNet.NET_NONE){
            Log.d("","网络不通");
            Toast.makeText(MainActivity.this,"网络不通",Toast.LENGTH_LONG).show();
        } else {
            Log.d("","网络OK");
            Toast.makeText(MainActivity.this,"网络OK",Toast.LENGTH_LONG).show();
        }


    }

    @Override
    public void setView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    public void initView() {


        //定义RadioButton数组用来装RadioButton，改变drawableTop大小
        rb = new RadioButton[4];
        mainMenu = (RadioGroup) findViewById(R.id.mainMenu);
        rb[0] = (RadioButton) findViewById(R.id.rb_device);  //社區首頁
        rb[1] = (RadioButton) findViewById(R.id.rb_discover);//生活欄目
        rb[2] = (RadioButton) findViewById(R.id.rb_hospital); //特色服務
        rb[3] = (RadioButton) findViewById(R.id.rb_medicine);  //商業推廣
        //for循环对每一个RadioButton图片进行缩放
        for (int i = 0; i < rb.length; i++) {
            //挨着给每个RadioButton加入drawable限制边距以控制显示大小
            drawables = rb[i].getCompoundDrawables();
            //获取drawables，2/5表示图片要缩小的比例
            Rect r = new Rect(0, 0, drawables[1].getMinimumWidth() * 2 / 5, drawables[1].getMinimumHeight() * 2 / 5);
            //定义一个Rect边界
            drawables[1].setBounds(r);
            //给每一个RadioButton设置图片大小
            rb[i].setCompoundDrawables(null, drawables[1], null, null);
        }
        vp_main = (NoScrollViewPager) findViewById(R.id.vp_main);
        vp_main.setOffscreenPageLimit(viewpageSize);
        vp_main.setAdapter(new PagerAdapter(getSupportFragmentManager()));
    }

    @Override
    public void setListener() {
        vp_main.setOnPageChangeListener(new PageChangeListener());
        mainMenu.setOnCheckedChangeListener(new MenuCheckedChangeListener());
    }

    private class PagerAdapter extends FragmentPagerAdapter {
        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                if (mHomeFragment == null) {
                    mHomeFragment = new HomeFragment();
                }
                return mHomeFragment;
            } else if (position == 1) {
                if (mLifestylesFragment == null) {
                    mLifestylesFragment = new LifestylesFragment();
                }
                return mLifestylesFragment;
            } else if (position == 2) {
                if (mServcesFragment == null) {
                    mServcesFragment = new ServcesFragment();
                }
                return mServcesFragment;
            } else {
                if (mCommercialFragment == null) {
                    mCommercialFragment = new CommercialFragment();
                }
                return mCommercialFragment;
            }
        }

        @Override
        public int getCount() {
            return viewpageSize;
        }
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            checkPreId = mainMenu.getCheckedRadioButtonId();
            switch (position) {
                case 0:
                    mainMenu.check(R.id.rb_device);
                    break;
                case 1:
                    mainMenu.check(R.id.rb_discover);
                    break;
                case 2:
                    mainMenu.check(R.id.rb_hospital);
                    break;
                case 3:
                    mainMenu.check(R.id.rb_medicine);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
    }

    private class MenuCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case R.id.rb_device:
                    vp_main.setCurrentItem(0);
                    break;
                case R.id.rb_discover:
                    vp_main.setCurrentItem(1);
                    break;
                case R.id.rb_hospital:
                    vp_main.setCurrentItem(2);
                    break;
                case R.id.rb_medicine:
                    vp_main.setCurrentItem(3);
                    break;
            }
        }
    }

}
